package aplication;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Endpoint;

import wsresource.client.Exception_Exception;
import wsresource.client.WSResource;
import wsresource.client.WSResourceService;

import wstm.client.WSTransactionManager;
import wstm.client.WSTransactionManagerService;

public class Aplication {
  public static void main(String[] args) throws Exception_Exception, MalformedURLException {

    //Publicar WS de la Aplicacion
    Publisher0 pub = new Publisher0();
    pub.publish();

    //Obtener servicio del transaction manager
    WSTransactionManagerService tmService = new WSTransactionManagerService();
    WSTransactionManager tm = tmService.getWSTransactionManagerPort();
    //------

    //Obtener servicio 0
    String wsRes0Url = "http://localhost:8086/WS/WSResource0?wsdl";
    URL baseUrl = wsresource.client.WSResourceService.class.getResource(".");
    URL url0 = new URL(baseUrl, wsRes0Url);
    WSResourceService resService0 = new WSResourceService( //
        url0, new QName("http://server.wsresource/", "WSResourceService"));
    WSResource wsRes0 = resService0.getWSResourcePort();
    //----

    //Obtener servicio 1
    String wsRes1Url = "http://localhost:8082/WS/WSResource1?wsdl";
    URL url1 = new URL(baseUrl, wsRes1Url);
    WSResourceService resService1 = new WSResourceService( //
        url1, new QName("http://server.wsresource/", "WSResourceService"));
    WSResource wsRes1 = resService1.getWSResourcePort();
    //----

    //Obtener servicio 2
    String wsRes2Url = "http://localhost:8084/WS/WSResource2?wsdl";
    URL url2 = new URL(baseUrl, wsRes2Url);
    WSResourceService resService2 = new WSResourceService( //
        url2, new QName("http://server.wsresource/", "WSResourceService"));
    WSResource wsRes2 = resService2.getWSResourcePort();
    //----

    String xid = tm.begin();

    System.out.println("APP: After begin " + xid);

    /*Prueba 1a
      String SQL_1_1 = "INSERT INTO item(nombre, cantidad) VALUES ('ItemD',40);";
      wsRes0.executeStatment(SQL_1_1, xid);
      wsRes1.executeStatment(SQL_1_1, xid);
      wsRes2.executeStatment(SQL_1_1, xid);
     */

    /*Prueba 1b
      String SQL_1_2a = "UPDATE item SET cantidad = 25 WHERE id = 2;";
      String SQL_1_2b = "UPDATE item SET cantidad = 35 WHERE id = 3;";
      String SQL_1_2c = "UPDATE item SET cantidad = 15 WHERE id = 1;";
      wsRes0.executeStatment(SQL_1_2a, xid);
      wsRes1.executeStatment(SQL_1_2b, xid);
      wsRes2.executeStatment(SQL_1_2c, xid);
     */

    /*Prueba 2a
      String SQL_2_1a = "INSERT INTO item(nombre, cantidad) VALUES ('ItemE',45);";
      String SQL_2_1b = "UPDATE item SET cantidad = 70 WHERE id = 1;";
      String SQL_2_1c = "UPDATE item SET cantidad = 15 WHERE id = 1;";
      wsRes0.executeStatment(SQL_2_1a, xid);
      wsRes1.executeStatment(SQL_2_1b, xid);
      wsRes2.executeStatment(SQL_2_1c, xid);
     */

    /*Prueba 2b
      String SQL_2_2a = "INSERT INTO item(nombre, cantidad) VALUES ('ItemE',45);";
      String SQL_2_2b = "UPDATE item SET cantidad = 15 WHERE id = 1;";
      String SQL_2_2c = "UPDATE item SET cantidad = 70 WHERE id = 1;";
      wsRes0.executeStatment(SQL_2_2a, xid);
      wsRes1.executeStatment(SQL_2_2b, xid);
      wsRes2.executeStatment(SQL_2_2c, xid);
     */

    /*Prueba 3
      String SQL_3a = "UPDATE item SET cantidad = 70 WHERE id = 1;";
      String SQL_3b = "INSERT INTO item(nombre, cantidad) VALUES ('ItemE',45);";
      String SQL_3c = "INSERT INTO item(nombre, cantidad) VALUES ('ItemE',45);";
      wsRes0.executeStatment(SQL_3a, xid);
      wsRes1.executeStatment(SQL_3b, xid);
      wsRes2.executeStatment(SQL_3c, xid);
     */

    //Prueba 4
    String SQL_4a = "UPDATE item SET cantidad = 70 WHERE id = 2;";
    String SQL_4b = "UPDATE item SET cantidad = 70 WHERE id = 3;";
    String SQL_4c = "UPDATE item SET cantidad = 70 WHERE id = 1;";
    wsRes0.executeStatment(SQL_4a, xid);
    wsRes1.executeStatment(SQL_4b, xid);
    wsRes2.executeStatment(SQL_4c, xid);

    System.out.println("APP: After execute statment ");

    if (!tm.commit(xid)) {
      System.err.println("APP: Error: No commit");
      tm.rollback(xid);
    }
    wsRes0.close(xid);
    wsRes1.close(xid);
    wsRes2.close(xid);
    System.out.println("APP: End");

    return;
  }
}
